package com.nantian.logic.controller;

import com.cuit.springboot.gdao.common.DictMapper;
import com.cuit.springboot.gentry.common.Dict;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nantian.logic.utils.TokenUtil;
import com.nantian.logic.utils.UniversalResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/dict")
public class DictController {
    @Autowired
    DictMapper dictMapper;
    @Autowired
    TokenUtil tokenUtil;

    /**
     * “活动列表”获取接口
     * 需要参数：分页参数(pageNo和pageSize)
     * 分页查询参考此处
     */
    @PostMapping(value = "/findAllDict")
    public UniversalResult findAllDict(@RequestBody Map<String, Integer> map, HttpServletRequest request) {
        PageHelper.startPage(map.get("pageNo"), map.get("pageSize"));
        PageInfo<Dict> page = new PageInfo<>(dictMapper.selectAll());
        return new UniversalResult<>(UniversalResult.SUCCESS, "查询成功", page);
    }

    @PostMapping(value = "/addDict")
    public UniversalResult<PageInfo<Dict>> addDict(@RequestBody Map<String, String> map) {
        Dict dictEntity = new Dict();
        dictEntity.setType(map.get("type"));
        dictEntity.setKey(map.get("key"));
        dictEntity.setValue(map.get("value"));
        dictEntity.setDescrip(map.get("descrip"));
        dictEntity.setDel(Byte.valueOf("0"));
        dictEntity.setOrder(Integer.valueOf(map.get("order")));
        int result = dictMapper.insert(dictEntity);
        System.out.println("插入字典：" + dictEntity + "  插入结果：" + result);
        PageHelper.startPage(1, 10);
        PageInfo<Dict> page = new PageInfo<>(dictMapper.selectBytype(map.get("type")));
        return new UniversalResult<>(UniversalResult.SUCCESS, "添加成功", page);
    }

    @PostMapping(value = "/findDict")
    public UniversalResult findDict(@RequestBody Map<String, String> map) {
        PageHelper.startPage(Integer.parseInt(map.get("pageNo")), Integer.parseInt(map.get("pageSize")));
        PageInfo<Dict> page;
        if ("".equals(map.get("type")) || null == map.get("type")) {
            page = new PageInfo<>(dictMapper.selectAll());
        } else {
            page = new PageInfo<>(dictMapper.selectBytype(map.get("type")));
        }
        return new UniversalResult<>(UniversalResult.SUCCESS, "查询成功", page);
    }

    @PostMapping(value = "/updateDict")
    public UniversalResult<PageInfo<Dict>> updateDict(@RequestBody Map<String, String> map) {
        Dict dictEntity = new Dict();
        dictEntity.setId(Integer.valueOf(map.get("id")));
        dictEntity.setType(map.get("type"));
        dictEntity.setKey(map.get("key"));
        dictEntity.setValue(map.get("value"));
        dictEntity.setDescrip(map.get("descrip"));
        dictEntity.setDel(Byte.valueOf("0"));
        dictEntity.setOrder(Integer.valueOf(map.get("order")));
        int result = dictMapper.updateByPrimaryKey(dictEntity);
        System.out.println("修改字典：" + dictEntity + "  修改结果：" + result);
        return new UniversalResult<>(UniversalResult.SUCCESS, "修改成功");
    }

    @PostMapping(value = "/deltDict")
    public UniversalResult<PageInfo<Dict>> deltDict(@RequestBody Map<String, String> map) {
        Dict dictEntity = dictMapper.selectByPrimaryKey(Integer.valueOf(map.get("id")));
        dictEntity.setDel(Byte.valueOf("1"));
        int result = dictMapper.updateByPrimaryKey(dictEntity);
        return new UniversalResult<>(UniversalResult.SUCCESS, "删除成功");
    }

    @PostMapping(value = "/findRedeemType")
    public UniversalResult<List<Dict>> findRedeemType(@RequestBody Dict dict) {
        List<Dict> list = dictMapper.selectBytype(dict.getType());
        return new UniversalResult<List<Dict>>(UniversalResult.SUCCESS, "查询积分规则成功", list);
    }

    @PostMapping(value = "/getKey")
    public UniversalResult<List<Map<String, String>>> getLabel(@RequestBody List<String> types) {
        if (types.size() > 0) {
            String where;
            StringBuilder sb = new StringBuilder();
            sb.append("SELECT `key`,type FROM dict WHERE type = '" + types.get(0) + "'");
            for (int i = 1; i < types.size(); i++) {
                sb.append("OR type = '" + types.get(i) + "'");
            }
            sb.append("ORDER BY `order` ASC");
            where = sb.toString();
            List<Map<String, String>> keys = dictMapper.selectKeyByTypes(where);

            return new UniversalResult<>(UniversalResult.SUCCESS, "查询成功", keys);
        }
        return new UniversalResult<>(UniversalResult.GETNULL, "参数不能为空");
    }
}
